<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

class functions {
	public $adminLoginForm = '
	<form action="" method="post" id="cms-form-login">
		<fieldset>
			<div class="form-row">
				<label for="username">Username: </label><input type="text" name="username" />
			</div>
			<div class="form-row">
				<label for="password">Password: </label><input type="password" name="password" />
			</div>
			<div class="form-row-last">
				<input type="submit" name="login" value="Login" />
			</div>
		</fieldset>
	</form>
	';
	public function __contruct () {
		$this->db_connect();
	}
	public function db_connect () {
		$mysql_con = mysql_connect(DBHOST.":".DBPORT, DBUSER, DBPASS);
		mysql_select_db(DBNAME, $mysql_con);
	}
	public function db_query($sql) {
		$queryStart = round(microtime(), 4);
		
		$result = mysql_query($sql);
		
		$GLOBALS['MYSQL_NUM_QUERIES']++;
		
		$queryEnd = round(microtime(), 4);
		
		$queryTime = $queryEnd - $queryStart;		
		
		$GLOBALS['MYSQL_QUERY_TIME'] += $queryTime;
		
		$call_info = array_shift( debug_backtrace() );
		$code_line = $call_info['line'];
		$file = array_pop( explode('/', $call_info['file']));
		$GLOBALS['mysql_debug'] .= '<strong>Ran Query #'.$GLOBALS['MYSQL_NUM_QUERIES']." (Line $code_line of ".basename($file).") (Time $queryTime): </strong>$sql<br />";
		
		return $result;
	}
	public function register_site_info () {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."settings_site";
		$query = $this->db_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($query)) {
			$this->page_info['settings'][$row['settingname']]['id'] = $row['id'];
			$GLOBALS['page_info']['settings'][$row['settingname']]['id'] = $row['id'];
			$this->page_info['settings'][$row['settingname']]['description'] = $row['description'];
			$GLOBALS['page_info']['settings'][$row['settingname']]['description'] = $row['description'];
			$this->page_info['settings'][$row['settingname']]['value'] = $row['value'];
			$GLOBALS['page_info']['settings'][$row['settingname']]['value'] = $row['value'];
		}
		define('HOME_PAGE_ID', $this->page_info['settings']['Home Page Id']['value']);
	}
	public function create_temp_file ($file, $x = 'w', $content) {
		$x = true;
		$file = BASE_URL.'temp/'.$file;
		echo $file;
		$fh = fopen($file, $x) or die(
			$y = "can't open file $file"
		);
		$data = $content;
		fwrite($fh, $data);
		fclose($fh);
		return $y;
	}
	public function deleteFile ($file) {
		if($file != "") {
			unlink(BASE_URL.'temp/'.$path);
			return true;
		}
		else
			return 'can"t open file';
	}
	public function openFile ($path, $relative=1) {
		if($relative == 0) {
			$content = file_get_contents(BASE_URL.$path); 
		}
		else {
			$content = file_get_contents($path); 
		}
	    return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
	}
	public function adminLoginForm () {
		return $adminLoginForm;
	}
	public function getSetting($name, $force = false) {
		if(isset($GLOBALS['page_info']['settings'][$name]) && $force == false) {
			return $GLOBALS['page_info']['settings'][$name]['value'];
		}
		else {
			$call_info = array_shift( debug_backtrace() );
			$code_line = $call_info['line'];
			$file = array_pop( explode('/', $call_info['file']));
			$GLOBALS['settingsDebug'] .= "Setting requested (Line $code_line of ".basename($file)."): </strong>$name<br />";
			
			
			//$GLOBALS['settingsDebug'] .= print_r($this->page_info);
			$sql = "SELECT * FROM ".DBTABLEPREFIX."settings_site WHERE settingname = '{$name}'";
			$query = $this->db_query($sql);
			$query = mysql_fetch_assoc($query);
			$this->page_info['settings'][$name]['value'] = $query['value'];
			return $this->page_info['settings'][$name]['value'];			
		}
	}
	public function prepareSettingArraysSmarty () {
		global $page_settings, $page_settings_system;
		//Remove spaces from settings arrays to be compatible with Smarty Templating System
		foreach($this->page_info['settings'] as $key => $value) {
			$key = str_replace(" ", "_", $key);
			$page_settings[$key] = $value;
		}
	}
	public function validate($if_fail_url="", $if_success_url="") {
		session_start();
		if(!isset($_SESSION['username'])) {
			if($if_fail_url!="") {
				header ("location: ".$if_fail_url."");
				exit();
			}
			return false;
		}
		$username = sanitize_sql_string($_SESSION['username']);
		$qresult = $this->db_query("SELECT * FROM ".DBTABLEPREFIX."users WHERE username = '".$username."'") or die(mysql_error());
		if (mysql_num_rows($qresult)!="1"){
			if($if_fail_url!="") {
			header ("location: ".$if_fail_url."");
			exit();
			}
			return false;
		}
		$dinfo=mysql_fetch_array($qresult);
		if ($dinfo['password'] == $_SESSION['password']) {
			if($if_success_url!=="") {
				$this->page_info['user']['value'] = $_SESSION['username'];
				header ("location: ".$if_success_url."");
				exit();
			}
			$this->page_info['user']['value'] = $_SESSION['username'];
			return true;
		}
		else {
			if($if_fail_url!=="") {
				header ("location: ".$if_fail_url."");
				exit();
			}
			return false;
		}		
	}
	public function dump ($str, $var = false) {
		if($var == true) {
			echo '<pre>';
			var_dump($str);
			echo '</pre>';
		}
		else {
			echo '<pre>';
			print_r($str);
			echo '</pre>';
		}
	}
	public function arrayToUl ($inputArray, $withKey = true) {
		$r .= ("<ul>");
		foreach($inputArray as $k=>$v) {
			if(is_array($v)) {
				$r .= "<li>$k";
				$this->arrayToUl($v,$withKey);
				$r .= "</li>";
			}
			else {
				if($withKey)
				$r .= "<li>$k : <b>$v</b></li>";
				else
				$r .= "<li>$v</li>";
			}
		}
		$r .= ("</ul>");
	}
	/*
		Message Functions
	*/
		public function message($type = 'message', $header = "", $content = "") {
			switch ($type) {
				case 'message':
					return "<div class='message-wrapper'><div class='message-message message'>\n\t<div class='message-message-header message-header'>".$header."</div>\n\t<div class='message-message-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div></div>";
					break;
				case 'success':
					return "<div class='message-wrapper'><div class='message-ok message'>\n\t<div class='message-ok-header message-header'>".$header."</div>\n\t<div class='message-ok-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div></div>";
					break;
				case 'error':
					return "<div class='message-wrapper'><div class='message-error message'>\n\t<div class='message-error-header message-header'>".$header."</div>\n\t<div class='message-error-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div></div>";
					break;
				case 'warning':
					return "<div class='message-wrapper'><div class='message-warning message'>\n\t<div class='message-warning-header message-header'>".$header."</div>\n\t<div class='message-warning-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div></div>";
					break;
			}
		}
		
}

?>